<template>
  <div class="forgot-password-page">
    <div class="login-wrapper">
      <div class="login-left">
        <div class="welcome-content">
          <h1>智护银龄</h1>
          <h2>社区智慧养老健康监护系统</h2>
          <p class="subtitle">为社区老年人提供全方位的健康监护服务</p>
          <div class="features">
            <div class="feature-item">
              <div class="feature-icon">
                <svg viewBox="0 0 24 24" width="24" height="24" fill="currentColor">
                  <path d="M12 21.35l-1.45-1.32C5.4 15.36 2 12.28 2 8.5 2 5.42 4.42 3 7.5 3c1.74 0 3.41.81 4.5 2.09C13.09 3.81 14.76 3 16.5 3 19.58 3 22 5.42 22 8.5c0 3.78-3.4 6.86-8.55 11.54L12 21.35z"/>
                </svg>
              </div>
              <div class="feature-text">
                <h3>实时健康监控</h3>
                <p>24小时监测老人健康数据</p>
              </div>
            </div>
            <div class="feature-item">
              <div class="feature-icon">
                <svg viewBox="0 0 24 24" width="24" height="24" fill="currentColor">
                  <path d="M1 21h22L12 2 1 21zm12-3h-2v-2h2v2zm0-4h-2v-4h2v4z"/>
                </svg>
              </div>
              <div class="feature-text">
                <h3>智能预警系统</h3>
                <p>异常情况及时预警通知</p>
              </div>
            </div>
            <div class="feature-item">
              <div class="feature-icon">
                <svg viewBox="0 0 24 24" width="24" height="24" fill="currentColor">
                  <path d="M12 12c2.21 0 4-1.79 4-4s-1.79-4-4-4-4 1.79-4 4 1.79 4 4 4zm0 2c-2.67 0-8 1.34-8 4v2h16v-2c0-2.66-5.33-4-8-4z"/>
                </svg>
              </div>
              <div class="feature-text">
                <h3>社区人员管理</h3>
                <p>统一管理社区老人信息</p>
              </div>
            </div>
          </div>
        </div>
      </div>
      <div class="login-right">
        <div class="login-container">
          <div class="login-header">
            <h2>{{ step === 1 ? '找回密码' : '重置密码' }}</h2>
            <p class="header-subtitle">{{ step === 1 ? '请输入您的账户信息以验证身份' : '请设置新的登录密码' }}</p>
          </div>
          
          <form v-if="step === 1" @submit.prevent="verifyUserInfo">
            <div class="form-group">
              <label for="username">用户名</label>
              <div class="input-wrapper">
                <svg viewBox="0 0 24 24" width="20" height="20" fill="currentColor">
                  <path d="M12 12c2.21 0 4-1.79 4-4s-1.79-4-4-4-4 1.79-4 4 1.79 4 4 4zm0 2c-2.67 0-8 1.34-8 4v2h16v-2c0-2.66-5.33-4-8-4z"/>
                </svg>
                <input 
                  v-model="userBackDTO.username" 
                  id="username" 
                  type="text" 
                  required 
                  placeholder="请输入用户名"
                />
              </div>
            </div>
            <div class="form-group">
              <label for="phone">手机号</label>
              <div class="input-wrapper">
                <svg viewBox="0 0 24 24" width="20" height="20" fill="currentColor">
                  <path d="M6.62 10.79c1.44 2.83 3.76 5.14 6.59 6.59l2.2-2.2c.27-.27.67-.36 1.02-.24 1.12.37 2.33.57 3.57.57.55 0 1 .45 1 1V20c0 .55-.45 1-1 1-9.39 0-17-7.61-17-17 0-.55.45-1 1-1h3.5c.55 0 1 .45 1 1 0 1.25.2 2.45.57 3.57.11.35.03.74-.25 1.02l-2.2 2.2z"/>
                </svg>
                <input 
                  v-model="userBackDTO.phone" 
                  id="phone" 
                  type="tel" 
                  required 
                  placeholder="请输入手机号"
                />
              </div>
            </div>
            <div class="form-group">
              <label for="realName">真实姓名</label>
              <div class="input-wrapper">
                <svg viewBox="0 0 24 24" width="20" height="20" fill="currentColor">
                  <path d="M12 12c2.21 0 4-1.79 4-4s-1.79-4-4-4-4 1.79-4 4 1.79 4 4 4zm0 2c-2.67 0-8 1.34-8 4v2h16v-2c0-2.66-5.33-4-8-4z"/>
                </svg>
                <input 
                  v-model="userBackDTO.realName" 
                  id="realName" 
                  type="text" 
                  required 
                  placeholder="请输入真实姓名"
                />
              </div>
            </div>
            <button type="submit" class="login-btn">
              <span>验证身份</span>
            </button>
          </form>

          <form v-else @submit.prevent="resetPassword">
            <div class="form-group">
              <label for="newPassword">新密码</label>
              <div class="input-wrapper">
                <svg viewBox="0 0 24 24" width="20" height="20" fill="currentColor">
                  <path d="M12 1L3 5v6c0 5.55 3.84 10.74 9 12 5.16-1.26 9-6.45 9-12V5l-9-4zm0 10.99h7c-.53 4.12-3.28 7.79-7 8.94V12H5V6.3l7-3.11v8.8z"/>
                </svg>
                <input 
                  v-model="updatePasswordDTO.password" 
                  id="newPassword" 
                  type="password" 
                  required 
                  placeholder="请输入新密码"
                />
              </div>
            </div>
            <div class="form-group">
              <label for="confirmPassword">确认密码</label>
              <div class="input-wrapper">
                <svg viewBox="0 0 24 24" width="20" height="20" fill="currentColor">
                  <path d="M12 1L3 5v6c0 5.55 3.84 10.74 9 12 5.16-1.26 9-6.45 9-12V5l-9-4zm0 10.99h7c-.53 4.12-3.28 7.79-7 8.94V12H5V6.3l7-3.11v8.8z"/>
                </svg>
                <input 
                  v-model="confirmPassword" 
                  id="confirmPassword" 
                  type="password" 
                  required 
                  placeholder="请再次输入新密码"
                />
              </div>
            </div>
            <button type="submit" class="login-btn">
              <span>重置密码</span>
            </button>
          </form>

          <div class="switch-login">
            <a @click="goToLogin">返回登录</a>
          </div>
          
          <p v-if="error" class="error">{{ error }}</p>
          <p v-if="success" class="success">{{ success }}</p>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import request from '../utils/request'

const router = useRouter()
const step = ref(1)
const error = ref('')
const success = ref('')
const confirmPassword = ref('')

const userBackDTO = reactive({
  username: '',
  phone: '',
  realName: ''
})

const updatePasswordDTO = reactive({
  username: '',
  password: ''
})

const verifyUserInfo = async () => {
  error.value = ''
  try {
    const res = await request.post('/user/back', userBackDTO)
    if (res.code === 1) {
      step.value = 2
      updatePasswordDTO.username = userBackDTO.username
      success.value = '身份验证成功，请设置新密码'
      // 清除成功消息，避免在下一步中显示
      setTimeout(() => {
        success.value = ''
      }, 3000)
    }
  } catch (e) {
    error.value = e.message || '验证失败，请检查输入信息'
    console.error('验证错误:', e)
  }
}

const resetPassword = async () => {
  error.value = ''
  
  // 检查密码确认
  if (updatePasswordDTO.password !== confirmPassword.value) {
    error.value = '两次输入的密码不一致'
    return
  }
  
  // 检查密码长度
  if (updatePasswordDTO.password.length < 6) {
    error.value = '密码长度不能少于6位'
    return
  }
  
  try {
    const res = await request.post('/user/back/Password', updatePasswordDTO)
    if (res.code === 1) {
      success.value = '密码重置成功，即将跳转到登录页面'
      // 3秒后跳转到登录页
      setTimeout(() => {
        router.push('/login')
      }, 3000)
    }
  } catch (e) {
    error.value = e.message || '密码重置失败，请稍后重试'
    console.error('重置密码错误:', e)
  }
}

const goToLogin = () => {
  router.push('/login')
}
</script>

<style scoped>
.forgot-password-page {
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.login-wrapper {
  display: flex;
  width: 100%;
  max-width: 1200px;
  height: 700px;
  background: rgba(255, 255, 255, 0.95);
  border-radius: 20px;
  overflow: hidden;
  box-shadow: 0 25px 50px rgba(0, 0, 0, 0.2);
}

.login-left {
  flex: 1;
  background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40px;
  position: relative;
  overflow: hidden;
}

.login-left::before {
  content: "";
  position: absolute;
  top: -50px;
  left: -50px;
  width: 200px;
  height: 200px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.1);
}

.login-left::after {
  content: "";
  position: absolute;
  bottom: -80px;
  right: -80px;
  width: 300px;
  height: 300px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.1);
}

.welcome-content {
  color: white;
  text-align: center;
  position: relative;
  z-index: 1;
  max-width: 500px;
}

.welcome-content h1 {
  font-size: 48px;
  font-weight: 700;
  margin-bottom: 10px;
  text-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

.welcome-content h2 {
  font-size: 28px;
  font-weight: 600;
  margin-bottom: 15px;
}

.subtitle {
  font-size: 18px;
  margin-bottom: 40px;
  opacity: 0.9;
}

.features {
  text-align: left;
}

.feature-item {
  display: flex;
  align-items: flex-start;
  margin-bottom: 30px;
}

.feature-icon {
  width: 50px;
  height: 50px;
  background: rgba(255, 255, 255, 0.2);
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 15px;
  flex-shrink: 0;
}

.feature-text h3 {
  font-size: 20px;
  font-weight: 600;
  margin-bottom: 5px;
}

.feature-text p {
  font-size: 16px;
  opacity: 0.9;
  margin: 0;
}

.login-right {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40px;
  background: white;
}

.login-container {
  width: 100%;
  max-width: 450px;
}

.login-header {
  text-align: center;
  margin-bottom: 40px;
}

.login-header h2 {
  font-size: 32px;
  color: #333;
  margin-bottom: 10px;
  font-weight: 600;
}

.header-subtitle {
  color: #666;
  font-size: 16px;
  margin: 0;
}

.form-group {
  margin-bottom: 25px;
}

label {
  display: block;
  margin-bottom: 10px;
  color: #333;
  font-weight: 500;
  font-size: 16px;
}

.input-wrapper {
  position: relative;
}

.input-wrapper svg {
  position: absolute;
  left: 15px;
  top: 50%;
  transform: translateY(-50%);
  color: #999;
}

.input-wrapper input {
  width: 100%;
  height: 55px;
  padding: 0 20px 0 50px;
  border: 2px solid #e1e5e9;
  border-radius: 12px;
  font-size: 16px;
  transition: all 0.3s;
  box-sizing: border-box;
}

.input-wrapper input:focus {
  border-color: #4facfe;
  outline: none;
  box-shadow: 0 0 0 3px rgba(79, 172, 254, 0.1);
}

.login-btn {
  width: 100%;
  height: 55px;
  background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
  color: white;
  border: none;
  border-radius: 12px;
  font-size: 18px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s;
  box-shadow: 0 4px 15px rgba(79, 172, 254, 0.3);
  margin-top: 10px;
}

.login-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(79, 172, 254, 0.4);
}

.login-btn:active {
  transform: translateY(0);
}

.switch-login {
  text-align: center;
  margin-top: 25px;
}

.switch-login a {
  color: #666;
  font-size: 16px;
  text-decoration: none;
  cursor: pointer;
  transition: color 0.3s;
}

.switch-login a:hover {
  color: #4facfe;
}

.error {
  color: #ff6b6b;
  text-align: center;
  margin-top: 20px;
  font-size: 16px;
  font-weight: 500;
}

.success {
  color: #52c41a;
  text-align: center;
  margin-top: 20px;
  font-size: 16px;
  font-weight: 500;
}

@media (max-width: 992px) {
  .login-wrapper {
    flex-direction: column;
    height: auto;
  }
  
  .login-left {
    padding: 30px 20px;
  }
  
  .login-right {
    padding: 30px 20px;
  }
  
  .welcome-content h1 {
    font-size: 36px;
  }
  
  .welcome-content h2 {
    font-size: 24px;
  }
}

@media (max-width: 576px) {
  .login-page {
    padding: 10px;
  }
  
  .login-wrapper {
    border-radius: 15px;
  }
  
  .login-header h2 {
    font-size: 28px;
  }
  
  .input-wrapper input {
    height: 50px;
    padding: 0 15px 0 45px;
  }
}
</style>